package com.chushouya.controller.manager.admin;


import com.chushouya.manager.dto.admin.role.AuthRoleDTO;
import com.chushouya.manager.dto.admin.role.RoleQuery;
import com.chushouya.manager.dto.admin.user.AdminUserListDTO;
import com.chushouya.manager.dto.admin.user.AdminUserQuery;
import com.chushouya.manager.dto.admin.user.AdminUserSaveDTO;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.enums.StatusEnum;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.manager.service.admin.RoleService;
import com.chushouya.manager.service.admin.UserRoleService;
import com.chushouya.manager.service.admin.AdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = ServiceType.PLATFORM + "用户管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/adminUser")
public class AdminUserController {

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private RoleService roleService;

    @Resource
    private UserRoleService userRoleService;

    /**
     * 获取用户列表
     */
    @ApiOperation("查询用户列表")
    @GetMapping("/list")
    public Response<PageResult<AdminUserListDTO>> list(AdminUserQuery query) {
        return Response.success(adminUserService.selectUserPageResult(query));
    }

    @ApiOperation("查询用户详情")
    @GetMapping("/getUserDetail")
    public Response<AdminUserSaveDTO> getUserDetail(Long adminUserId) {
        return Response.success(adminUserService.getUserDetail(adminUserId));
    }


    @ApiOperation("新增用户")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Response<Long> add(@Validated @RequestBody AdminUserSaveDTO adminUser) {
        return Response.success(adminUserService.saveUser(adminUser));
    }

    @ApiOperation("编辑用户")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<Long> edit(@Validated @RequestBody AdminUserSaveDTO adminUser) {
        return Response.success(adminUserService.saveUser(adminUser));
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminUserId", value = "Admin用户Id", dataType = "Long", paramType = "query")
    })
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Response<Void> remove(Long adminUserId) {
        if (adminUserService.isSuperAdmin(adminUserId)) {
            throw Ex.business("当前用户不能删除");
        }
        adminUserService.deleteUser(adminUserId);
        return Response.success();
    }

    @ApiOperation("重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminUserId", value = "Admin用户Id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "新密码", dataType = "String", paramType = "query")
    })
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/resetPassword")
    public Response<Void> resetPassword(Long adminUserId, String password) {
        adminUserService.checkUserAllowed();
        adminUserService.resetPassword(adminUserId, password);
        return Response.success();
    }


    @ApiOperation("状态修改")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "adminUserId", value = "Admin用户Id", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "status", value = "状态(1正常,0停用)", dataType = "Integer", paramType = "query")
    })
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public Response<Void> changeStatus(Long adminUserId, Integer status) {
        adminUserService.checkUserAllowed();
        adminUserService.changeStatus(adminUserId, status);
        return Response.success();
    }

    @ApiOperation("获取用户当前角色和角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminUserId", value = "Admin用户Id", dataType = "Long", paramType = "query")
    })
    @GetMapping("/authRole")
    public Response<AuthRoleDTO> authRole(Long adminUserId) {
        AuthRoleDTO authRole = new AuthRoleDTO();
        authRole.setRoleIdList(userRoleService.selectUserRoleIdList(adminUserId));
        RoleQuery query = new RoleQuery();
        query.setStatus(StatusEnum.normal.value());
        authRole.setRoleList(roleService.selectRoleList(query));
        return Response.success(authRole);
    }

    @ApiOperation("用户授权角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminUserId", value = "Admin用户Id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "roleIdList", value = "角色IdList", dataType = "List", paramType = "query")
    })
    @Log(businessType = BusinessType.GRANT)
    @PostMapping("/grantRole")
    public Response<Void> grantRole(Long adminUserId, Long[] roleIdList) {
        adminUserService.checkUserAllowed();
        adminUserService.grantRole(adminUserId, Lists.asList(roleIdList));
        return Response.success();
    }

}
